<?php

namespace Admin\Controller;
use Think\Controller;

class StatisticsController extends Controller {

    //生成每日广告商订单列表统计
    public function day_advertisers(){  
        $start_time = strtotime(date('Ymd'));  //当前时间开始的零点
        $date = $start_time-60*60*24;  //上一天开始的零点

        $where = "table_order.payment_order>=$date && table_order.payment_order<$start_time";  //查询从当天零点开始到上一天零点开始

        $order = M('order');
        $model = M();
        $advertisers = $order->join("table_advertisers on table_order.ad_id=table_advertisers.id")->where($where)->select();

        if(empty($advertisers)){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "今日没有订单信息";
            $this->ajaxReturn($ajaxReturn);
        }
        foreach($advertisers as $key=>$v){
            $a[$key] = $v;
            
        }
        $ab = 0;
        foreach($a as $key=>$v){
            foreach($a as $k=>$s){
                if($v['game_id']==$s['game_id']){
                    $ab+=$s['order_price'];
                    $gameid = $s['game_id'];
                    $ad_id = $s['ad_id'];
                    $game_name = $s['game_name'];
                    $login_name = $s['login_name'];
                    $game_id = $s['game_id'];
                    $ch_id = $s['ch_id'];
                }
            }

            //计算百分比
            $bai = $v['layered'];
            $layered = $ab*$bai/100;

            $arr[$key]['date_per_day'] = $date;
            $arr[$key]['game_name'] = $game_name;
            $arr[$key]['login_name'] = $login_name;
            $arr[$key]['order_price'] = $ab;
            $arr[$key]['layered'] = $layered;
            $arr[$key]['ad_id'] = $ad_id;
            $arr[$key]['game_id'] = $game_id;
            $arr[$key]['layereds'] = $v['layered'];
            $arr[$key]['ch_id'] = $ch_id;
            $ab = 0;
                
        }

        foreach ($arr as $v){
          $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
          $temp[]=$v;
         }
         $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
         foreach ($temp as $k => $v){
          $temp[$k]=explode(',',$v); //再将拆开的数组重新组装  
         }
        $temp = array_merge($temp);
        $sql = '';

        foreach($temp as $key=>$v){
            $sql.="('".$v[0]."','".$v[1]."','".$v[2]."','".$v[3]."','".$v[4]."','".$v[5]."',".$v[6].",".$v[7].",".$v[8]."),";
        }

        $sql1 = substr($sql,0,strlen($sql)-1); 
        //批量操作
        $res = $model->execute("insert into table_game_statistics(date_per_day,gname,advertiser_name,money_everyday,layered_money,ad_id,game_id,layered,ch_id) values".$sql1);
        if($res){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "广告主每天统计出错";
        }

        $this->ajaxReturn($ajaxReturn);
        
    }

    //生成每日渠道商订单列表统计
    public function day_channel(){
        $start_time = strtotime(date('Ymd'));  //当前时间开始的零点
        $date = $start_time-60*60*24;  //上一天开始的零点
        $where = "table_order.payment_order>=$date and table_order.payment_order<$start_time and order_status=1 || order_status=2";
        $order = M('order');
        $model = M();
        $advertisers = $order->join("table_channel on table_order.ch_id=table_channel.id")->where($where)->select();

        if(empty($advertisers)){
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "今日没有订单信息";
            $this->ajaxReturn($ajaxReturn);
        }
        foreach($advertisers as $key=>$v){
            $a[$key]= $v;
            
        }

        $a = array_merge($a);
        $ab = 0;
        foreach($a as $key=>$v){
            foreach($a as $k=>$s){
                if($v['game_id']==$s['game_id'] && $v['ch_id']==$s['ch_id']){
  

                        $ab+=$s['order_price'];
                        $gameid = $s['game_id'];
                        $ch_id = $s['ch_id'];
                        $game_name = $s['game_name'];
                        $channel_name = $s['channel_name'];
                        $game_id = $s['game_id'];
                
                }
            }

                if($v['skimp']==0){
                    $layered = 0;
                }else{
                    $layered = $ab*$v['layered']/100;
                }

                $arr[$key]['date_per_day'] = $date;
                $arr[$key]['channel_name'] = $channel_name;
                $arr[$key]['gname'] = $game_name;
                
                $arr[$key]['money_everyday'] = $ab;
                $arr[$key]['layered_money'] = $layered;
                $arr[$key]['ch_id'] = $ch_id;
                $arr[$key]['game_id'] = $gameid;
            //计算百分比
            $ab = 0;   
        }
        
        //print_r($arr);die;
        //print_r($arr);die;
        foreach ($arr as $v){
          $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
          $temp[]=$v;
         }
         $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
         foreach ($temp as $k => $v){
          $temp[$k]=explode(',',$v); //再将拆开的数组重新组装
         }
        $temp = array_merge($temp);
        //print_r($temp);die;
        foreach($temp as $key=>$v){
            foreach($advertisers as $k=>$s){

                if($v[6]==$s['game_id']){
                    if($s['skimp']==0){
                        $ab+=$s['order_price'];
                        $str[$key]['order_layered'] = $ab*$s['layered']/100;

                    }
                }
            }
            $ab = 0;
        }



        foreach($temp as $key=>$v){

            $temp[$key][7] = $str[$key]['order_layered'];
        }

        foreach($temp as $k=>$v){
            if($v[4]==0){
                unset($temp[$k]);
            }else{
                if($v[7]!=0){
                    $temp[$k][4] = $v[4]-$v[7];
                }
            }
        }
            //unset($temp);
        $temp = array_merge($temp);

        //print_r($temp);die;
        $sql = '';
        foreach($temp as $key=>$v){
            $sql.="('".$v[0]."','".$v[1]."','".$v[2]."','".$v[3]."','".$v[4]."','".$v[5]."','".$v[7]."','".$v[6]."'),";
        }   
        $sql1 = substr($sql,0,strlen($sql)-1);
        //批量插入操作
        $res = $model->execute("insert into table_channel_statistics(date_per_day,channel_name,gname,money_everyday,layered_money,ch_id,reduce_money,game_id) values".$sql1);
        if($res){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "渠道每天统计出错";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    //广告主充值列表
    public function day_advertisers_list(){
        $page = I("post.page");
        $page_size = I("post.page_size");
        //$ad_id = I('post.ad_id');
        $where='';
        //判断是否是非后台管理员操作
        if($ad_id){
            $where.="ad_id=$ad_id";
        }
        $order = M('game_statistics');
        $count = $order->where($where)->count();  //总条数
        $total =  ceil($count / $page_size);  //总页数
        $limitStart = ($page-1) * $page_size; //偏移量
        
        $result = $order->order("id DESC")->where($where)->limit($limitStart,$page_size)->select();

        $data['page']['total_page'] = $total;
        $data['list'] = $result;

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;
    
        $this->ajaxReturn($ajaxReturn);
    }

    //渠道商充值列表
    public function day_channel_list(){
        $page = I("post.page");
        $page_size = I("post.page_size");
        
        $ch_id = I('post.ch_id');
        $where = '';
        //判断是否是非后台管理员操作
        if($ch_id){
            $where.="ch_id=$ch_id";
        }

        $order = M('channel_statistics');

        $count = $order->count();  //总条数
        $total =  ceil($count / $page_size);  //总页数
        $limitStart = ($page-1) * $page_size; //偏移量
        
        $result = $order->order("date_per_day DESC")->where($where)->limit($limitStart,$page_size)->select();

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        
        $this->ajaxReturn($ajaxReturn);
    }

    //广告商搜索充值列表
    public function day_advertisers_search(){
        $user_id = I('post.user_id');
        $game_id = I('post.game_id');
        $start_time = I('post.start_time');
        $end_time = I('post.end_time');
        $page = I("post.page");
        $page_size = I("post.page_size");

        $where = '1';
        if($user_id){
            $where.=" and ad_id=$user_id";
        }
        if($game_id){
            $where.=" and game_id=$game_id";
        }
        if($start_time){
            $where.=" and date_per_day>=$start_time";
        }
        if($end_time){
            $where.=" and date_per_day<=$end_time";
        }
        $game = M('game_statistics');

        $count = $game->where($where)->count();  //总条数
        $total =  ceil($count / $page_size);  //总页数
        $limitStart = ($page-1) * $page_size; //偏移量
        
        $result = $game->order("id DESC")->where($where)->limit($limitStart,$page_size)->select();


        if($result){
            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{
            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }

        $this->ajaxReturn($ajaxReturn);
    }

    //渠道商搜索充值列表
    public function day_channel_search(){
        $user_id = I('post.user_id');
        $game_id = I('post.game_id');
        $start_time = I('post.start_time');
        $end_time = I('post.end_time');
        $page = I("post.page");
        $page_size = I("post.page_size");
        $where = '1';
        if($user_id){
            $where.=" and ch_id=$user_id";
        }
        if($game_id){
            $where.=" and game_id=$game_id";
        }
        if($start_time){
            $where.=" and date_per_day>=$start_time";
        }
        if($end_time){
            $where.=" and date_per_day<=$end_time";
        }

        $channel = M('channel_statistics');

        $count = $channel->where($where)->count();  //总条数
        $total =  ceil($count / $page_size);  //总页数
        $limitStart = ($page-1) * $page_size; //偏移量
        
        $result = $channel->order("date_per_day DESC")->where($where)->limit($limitStart,$page_size)->select();

        $data['page']['total_page'] = $total;
        $data['list'] = $result;

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";
        $ajaxReturn['data'] = $data;

        $this->ajaxReturn($ajaxReturn);
    }


    //获取广告主列表
    public function advertisers(){
        $data = M('advertisers')->field('id,corporate_name')->where("status=1")->select();
        if($data){
            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{
            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "还没有记录";
        }

        $this->ajaxReturn($ajaxReturn);
    }

}